<!DOCTYPE html>
<html lang="zh-cn">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    
    <title>源码阅读：node-only | liuz2&#39;s Blog</title>
    <meta name="viewport" content="width=device-width,minimum-scale=1">
    <meta name="generator" content="Hugo 0.63.1" />
    
    
      <META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
    

    
    
      <link href="/dist/css/app.9576afd3b3c158d98f48ec5df01fec6a.css" rel="stylesheet">
    

    

    
      

    

    

    <meta property="og:title" content="源码阅读：node-only" />
<meta property="og:description" content="阅读 node-only 的源码" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://liuz2.gitee.io/posts/source-node-only/" />
<meta property="article:published_time" content="2021-12-06T20:30:48+08:00" />
<meta property="article:modified_time" content="2021-12-06T20:30:48+08:00" />
<meta itemprop="name" content="源码阅读：node-only">
<meta itemprop="description" content="阅读 node-only 的源码">
<meta itemprop="datePublished" content="2021-12-06T20:30:48&#43;08:00" />
<meta itemprop="dateModified" content="2021-12-06T20:30:48&#43;08:00" />
<meta itemprop="wordCount" content="92">



<meta itemprop="keywords" content="源码," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="源码阅读：node-only"/>
<meta name="twitter:description" content="阅读 node-only 的源码"/>

	
  </head>

  <body class="ma0 avenir bg-white">

    
   
  

  <header>
    <div class="bg-black">
      <nav class="pv3 ph3 ph4-ns" role="navigation">
  <div class="flex-l justify-between items-center center">
    <a href="/" class="f3 fw2 hover-white no-underline white-90 dib">
      
        liuz2&#39;s Blog
      
    </a>
    <div class="flex-l items-center">
      

      
        <ul class="pl0 mr3">
          
          <li class="list f5 f4-ns fw4 dib pr3">
            <a class="hover-white no-underline white-90" href="/about/" title="About page">
              About
            </a>
          </li>
          
          <li class="list f5 f4-ns fw4 dib pr3">
            <a class="hover-white no-underline white-90" href="/links/" title="Links page">
              Links
            </a>
          </li>
          
          <li class="list f5 f4-ns fw4 dib pr3">
            <a class="hover-white no-underline white-90" href="/timeline/" title="Timeline page">
              Timeline
            </a>
          </li>
          
        </ul>
      
      















    </div>
  </div>
</nav>

    </div>
  </header>



    <main class="pb7" role="main">
      
  
  <article class="flex-l flex-wrap justify-between mw8 center ph3">
    <header class="mt4 w-100">
      <h1 class="f3 athelas mt3 mb1">源码阅读：node-only</h1>
      
      
      <time class="f6 mv1 dib tracked" datetime="2021-12-06T20:30:48&#43;08:00">December 6, 2021</time>

      
      
    </header>
    <div class="nested-copy-line-height lh-copy sans-serif f5 nested-links nested-img mid-gray pr4-l w-two-thirds-l"><h2 id="介绍">介绍</h2>
<p><a href="https://www.npmjs.com/package/only" title="only - npm">only</a> 是 Koa 框架的依赖之一，作者是 <a href="https://github.com/tj" title="TJ">TJ</a>。</p>
<p>only 用于筛选对象的特定属性。比如：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-js" data-lang="js"><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">obj</span> <span style="color:#f92672">=</span> {
    <span style="color:#a6e22e">name</span><span style="color:#f92672">:</span> <span style="color:#e6db74">&#39;tobi&#39;</span>,
    <span style="color:#a6e22e">last</span><span style="color:#f92672">:</span> <span style="color:#e6db74">&#39;holowaychuk&#39;</span>,
    <span style="color:#a6e22e">email</span><span style="color:#f92672">:</span> <span style="color:#e6db74">&#39;tobi@learnboost.com&#39;</span>,
    <span style="color:#a6e22e">_id</span><span style="color:#f92672">:</span> <span style="color:#e6db74">&#39;12345&#39;</span>,
};
<span style="color:#66d9ef">const</span> <span style="color:#a6e22e">user</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">only</span>(<span style="color:#a6e22e">obj</span>, <span style="color:#e6db74">&#39;name last email&#39;</span>);

<span style="color:#75715e">// 输出如下内容：
</span><span style="color:#75715e"></span>{
    <span style="color:#a6e22e">name</span><span style="color:#f92672">:</span> <span style="color:#e6db74">&#39;tobi&#39;</span>,
    <span style="color:#a6e22e">last</span><span style="color:#f92672">:</span> <span style="color:#e6db74">&#39;holowaychuk&#39;</span>,
    <span style="color:#a6e22e">email</span><span style="color:#f92672">:</span> <span style="color:#e6db74">&#39;tobi@learnboost.com&#39;</span>
}
</code></pre></div><h2 id="源码">源码</h2>
<p><code>node-only</code> 的源码只有一个文件 <a href="https://github.com/tj/node-only/blob/master/index.js" title="node-only/index.js at master">index.js</a>:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-js" data-lang="js"><span style="color:#a6e22e">module</span>.<span style="color:#a6e22e">exports</span> <span style="color:#f92672">=</span> <span style="color:#66d9ef">function</span> (<span style="color:#a6e22e">obj</span>, <span style="color:#a6e22e">keys</span>) {
    <span style="color:#a6e22e">obj</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">obj</span> <span style="color:#f92672">||</span> {};
    <span style="color:#66d9ef">if</span> (<span style="color:#e6db74">&#39;string&#39;</span> <span style="color:#f92672">==</span> <span style="color:#66d9ef">typeof</span> <span style="color:#a6e22e">keys</span>) <span style="color:#a6e22e">keys</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">keys</span>.<span style="color:#a6e22e">split</span>(<span style="color:#e6db74">/ +/</span>);
    <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">keys</span>.<span style="color:#a6e22e">reduce</span>(<span style="color:#66d9ef">function</span> (<span style="color:#a6e22e">ret</span>, <span style="color:#a6e22e">key</span>) {
        <span style="color:#66d9ef">if</span> (<span style="color:#66d9ef">null</span> <span style="color:#f92672">==</span> <span style="color:#a6e22e">obj</span>[<span style="color:#a6e22e">key</span>]) <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">ret</span>;
        <span style="color:#a6e22e">ret</span>[<span style="color:#a6e22e">key</span>] <span style="color:#f92672">=</span> <span style="color:#a6e22e">obj</span>[<span style="color:#a6e22e">key</span>];
        <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">ret</span>;
    }, {});
};
</code></pre></div><p>注意细节处理：</p>
<ol>
<li>白名单 <code>keys</code> 可以是数组，也可以是字符串。</li>
<li>当为字符串类型，会用 <code>split()</code> 拆分为数组。为了处理多个连续空格符，使用了正则表达式 <code>/ +/</code>，而不是简单的 <code>' '</code>。</li>
</ol>
<a href="https://gitee.com/liuz2/liuz2/blob/master/content/posts/source-node-only.md#tree_comm_title" class="no-underline db b--black pa2 tc bg-black-90" style="color:#eee;">进入留言</a><ul class="pa0">
  
   <li class="list dib">
     <a href="/tags/%E6%BA%90%E7%A0%81" class="link f5 grow no-underline br-pill ba ph3 pv2 mb2 dib black sans-serif">源码</a>
   </li>
  
</ul>
<div class="mt6 instapaper_ignoref">
      
      
      </div>
    </div>

    <aside class="w-30-l mt6-l">




</aside>

  </article>

    </main>
    <footer class="bg-black bottom-0 w-100 pa3" role="contentinfo">
  <div class="flex justify-between">
  <a class="f4 fw4 hover-white no-underline white-70 dn dib-ns pv2 ph3" href="https://liuz2.gitee.io/" >
    &copy;  liuz2's Blog 2022 
  </a>
    <div>














</div>
  </div>
</footer>

    

  <script src="/dist/js/app.3fc0f988d21662902933.js"></script>


  </body>
</html>
